package com.oops.component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.oops.constants.Constants;
import com.oops.mapper.TGoodsMapper;
import com.oops.model.TGoods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * TODO: 商品信息初始化到缓存中
 *
 * @author wuyijie
 * @date 2022/4/16 19:16
 */
@Slf4j
@Component
public class GoodsInitialize implements CommandLineRunner {

    private final TGoodsMapper tGoodsMapper;
    private final StringRedisTemplate stringRedisTemplate;

    public GoodsInitialize(TGoodsMapper tGoodsMapper, StringRedisTemplate stringRedisTemplate) {
        this.tGoodsMapper = tGoodsMapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void run(String... args) {
        List<TGoods> tGoods = tGoodsMapper.selectList(new LambdaQueryWrapper<>(TGoods.class));
        TGoods goods = tGoods.get(0);
        stringRedisTemplate.opsForValue().set(Constants.SECKILL_GOODS_PREFIX+goods.getId(), String.valueOf(goods.getInventory()));
        log.info("初始化商品库存成功：{}",goods.getInventory());
    }
}
